package uk.org.landeg.euler.problems;

import java.util.Set;

import uk.org.landeg.euler.PrimeGen;
import uk.org.landeg.euler.problems.framework.AbstractProblem;
import uk.org.landeg.euler.problems.framework.ProblemLauncher;

public class Problem071 extends AbstractProblem {

	public static void main(String[] args) {
		ProblemLauncher launcher = new ProblemLauncher(new Problem071());
		launcher.solve();
	}

	@Override
	public Object solve() {
		double closest = 1;
		int cd=1, cn=1;
		int d,n;
		double target = 3.0/7.0;
		double diff,eval;
		double unit;
		PrimeGen.generate(1000000);
		Set<Long> primes = PrimeGen.getPrimes();
		for (Long pr : primes) {
			d = pr.intValue();
			unit = 1 / d;
			n = (int)d / 7 * 3;
			eval = (double)n/(double)d;
			while (eval > target) {
				n--;
				eval = (double)n/(double)d;
			}
			diff = target - eval;
			if (diff > 0) {
				if (diff < closest) {
					closest = diff;
					cd = d;
					cn = n;
					System.out.println(String.format("%d/%d\t%15.12f\t%15.12f",cn,cd,eval,diff));
				}
			}
		}
		System.out.println(PrimeGen.factorise(cn));
		System.out.println(PrimeGen.factorise(cd));
		return new Integer(cn);
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return "Listing reduced proper fractions in ascending order of size.";
	}

	@Override
	public Integer getProblemNumber() {
		// TODO Auto-generated method stub
		return 71;
	}

}
